package com.longe.security;

import com.longe.entity.MyPrincipal;
import com.longe.service.UserService;
import com.longe.util.JwtUtil;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;

    @Autowired
    private JwtUtil jwtUtil;

    /**当前ream的名称**/
    @Override
    public void setName(String name) {
        super.setName("currentReamName");
    }

    /**
     * 必须重写此方法，不然Shiro会报错
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 认证
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        JwtToken jwtToken = (JwtToken) authenticationToken;
        /**我这里认为能正常解析出来token就是合法的请求。当然你也可以进一步的验证,比如根据token中的信息和数据库中进行比对**/
        String userName = jwtUtil.getUserName(jwtToken.getPrincipal().toString());

        if(null == userName){
            throw new RuntimeException("非法的token");
        }

        //根据用户的情况, 来构建 AuthenticationInfo 对象并返回. 通常使用的实现类为: SimpleAuthenticationInfo
        //以下信息是从数据库中获取的.
        //1). principal: 认证的实体信息. 可以是 username, 也可以是数据表对应的用户的实体类对象.
        MyPrincipal myPrincipal = new MyPrincipal();
        myPrincipal.setUserName(userName);
        myPrincipal.setToken(jwtToken.getPrincipal().toString());
        Object principal = myPrincipal;
        //2). credentials.
        Object credentials = jwtToken.getCredentials();
        //3). realmName: 当前 realm 对象的 name.
        String realmName = getName();

        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, realmName);
        return info;
    }

    /**
     * 授权
     * @param principalCollection 包含了所有已认证安全数据
     * @return AuthorizationInfo 授权数据
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        /**这里为了简单演示,就不从数据库中获取了**/
        MyPrincipal myPrincipal = (MyPrincipal) principalCollection.getPrimaryPrincipal();
        //可以根据userName到数据库中查询相关权限，这里就不从数据库中查询了
        String userName = jwtUtil.getUserName(myPrincipal.getToken());
        if(null == userName){
            throw new RuntimeException("非法的token");
        }

        Set<String> roles = new HashSet<>();
        roles.add("user");

        Set<String> permissions = new HashSet<>();
        permissions.add("customer:*");
        permissions.add("goods:detail");
        permissions.add("/customize/**");

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        info.setRoles(roles);
        info.setStringPermissions(permissions);
        return info;
    }

    /**
     * 清除当前用户的授权缓存(感觉没什么用，管理员肯定清除其他用户的缓存。)
     * @param principals
     */
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }

    /**
     * 清除当前用户的认证和授权缓存(感觉没什么用，管理员肯定清除其他用户的缓存。)
     * @param principals
     */
    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }

    /**
     * 清除所有用户的认证和授权缓存
     */
    public void clearAll(){

        //清空认证缓存
        //getAuthenticationCache().clear();
        //清空授权缓存
        getAuthorizationCache().clear();
    }
}
